<?php $textdata = array(
              'name'        => 'text',
              'id'          => 'text',
              'value'       => '',
              'rows'		=> '3',
              'style'       => 'width:40%; resize: none;',
            );?>     


<script type="text/javascript">
	$(document).ready(function(){handlers();$("#subscribe").click(subscribe);});

	function handlers(){
		$("#bookmark").click(bookmark);
		$("#bookmarked").click(unbookmark);
		
	};
	function bookmark(){
		$.ajax({
			type: "POST",
			url: "<?php echo base_url()?>index.php/articles/bookmarkArticle",
			data: "userid=<?php echo $this->user_model->getUserIdFromSession()?>&articleid=<?php echo $articleID?>",
			success: function(){
						$("#bookmark").empty();
						$("#bookmark").append('<?php echo img(array('src'=>'images/icons/book_delete.png', 'alt'=>'Remove Bookmark', 'title'=>'Remove bookmark'));?>');
						$("#bookmark").unbind("click", bookmark);
						$("#bookmark").attr('id', 'bookmarked');					
						handlers();		
					}
			});
		};
	function unbookmark(){
		$.ajax({
			type: "POST",
			url: "<?php echo base_url()?>index.php/articles/unbookmarkArticle",
			data: "userid=<?php echo $this->user_model->getUserIdFromSession()?>&articleid=<?php echo $articleID?>",
			success: function(){
						$("#bookmarked").empty();
						$("#bookmarked").append('<?php echo img(array('src'=>'images/icons/book_add.png', 'alt'=>'Bookmark', 'title'=>'Bookmark article'));?>');
						$("#bookmarked").unbind("click", unbookmark);
						$("#bookmarked").attr('id', 'bookmark');					
						handlers();
					}
			});
	};

	function subscribe(){
		$.ajax({
			type: "POST",
			url: "<?php echo base_url()?>index.php/articles/subscribe",
			data: "userid=<?php echo $this->user_model->getUserIdFromSession()?>&authorid=<?php echo $article->userID?>",
			success: function(){
						$("#subscribe").empty();
						$("#subscribe").append('<?php echo img('images/icons/bell_go.png');?>');
						$("#subscribe").unbind("click", subscribe);
						$("#subscribe").removeAttr("style");
						$("#subscribe").attr("id", "subscribed");
				}
		});
	}

	$(document).ready(function() {

		$('[class^=star_]').mouseenter(
	        function() {
	            if($(this).parent().data('selected') == undefined) {
	                var selectedStar = $(this).parent().find('.hover_star').length - 1;
	                $(this).parent().data('selected', selectedStar)
	            }
	            $(this).children().addClass('hover_star');
	            $(this).prevAll().children().addClass('hover_star');
	            $(this).nextAll().children().removeClass('hover_star');
	        });
	
	    $('[id^=rating_]').mouseleave(
	        function() {
	            var selectedIndex = $(this).data('selected');
		
				if  (selectedIndex == -1)
				{
					$(this).children("[class^=star_]").children('img').removeClass("hover_star");
				}
				
	            var $selected = $(this).find('img').eq(selectedIndex).addClass('hover_star').parent();		
	            $selected.prevAll().children().addClass('hover_star');
	            $selected.nextAll().children().removeClass('hover_star');
    });


		$("[id^=rating_]").children("[class^=star_]").click(function() 
		{
			var current_star = $(this).attr("class").split("_")[1];
			var rid = $(this).parent().attr("id").split("_")[1];
	
			$('#star_container_'+rid).load('<?php echo site_url() ?>/articles/submitRating', {rating: current_star, id: rid});
	
		});
});

</script>

<div id="main_content">
	
	<?php if(isset($article)) :?>
	<h1><?php echo $article->title; ?></h1>
		<div id="article">
		<table class="article_Table">
		<tr>
		<?php if(!($article->imgRaw == '')):?>
		<td class="article_Img"><?php echo "<a href=".base_url().'userimages/'.$article->imgRaw.$article->imgExt.'>'."<img src=".base_url().'userimages/thumbs/'.$article->imgRaw.'_thumb'.$article->imgExt.' border="0"'.'></a>';?></td>
		<?php endif;?>
		</tr>
		<tr>
		<td class="article_Title"><?php //echo anchor("articles/id/$row->articleID", $row->title); ?></td>
		</tr>
		<tr>
		<td class="article_Author_and_Date"><?php echo "Written by ".$this->user_model->getUsername($article->userID).' '.$article->date; ?></td>
		<?php if(isset($isLoggedIn)):?>
			<?php if($isLoggedIn):?>
				<?php if($article->userID == $this->user_model->getUserIdFromSession()):?>
					<td class="article_bookmark">
						<?php echo anchor(base_url().'index.php/articles/edit/'.$article->articleID, img('images/icons/pencil.png'));?>
					</td>
				<?php else:?>
					<td>wtf?</td>
				<?php endif;?>
				<?php if(!$bookmarked):?>
					<td id="bookmark" class="article_bookmark" style="cursor:pointer;">
						<?php echo img(array('src'=>'images/icons/book_add.png', 'alt'=>'Bookmark', 'title'=>'Bookmark article'));?>
					</td>
				<?php else:?>
					<td id="bookmarked" class="article_bookmark" style="cursor:pointer;">
						<?php echo img(array('src'=>'images/icons/book_delete.png', 'alt'=>'Remove Bookmark', 'title'=>'Remove bookmark'));?>
					</td>
				<?php endif;?>
				<?php if(!$subscribed):?>
					<td id="subscribe" class="article_subscription" style="cursor:pointer;"><?php echo img('images/icons/bell_add.png');?></td>
				<?php else:?>
					<td id="subscribed" class="article_subscription"><?php echo img('images/icons/bell_go.png');?></td>
				<?php endif;?>
			<?php endif;?>
		<?php endif;?>
		</tr>
		<tr>
		<td class="article_Text" colspan="0"><?php echo $article->text; ?></td>
		</tr>
		</table>
		</div>
		
			<div class="floatleft">
				<div id="star_container_<?php echo $articleID ?>">
					<div id="rating_<?php echo $articleID ?>">
						<?php //echo "Rate:" ?>
						<span class="star_1"><img src="<?php echo base_url() ?>images/star_blank.png" alt="" <?php if($ratingAvg > 0) { echo"class='hover_star'"; } ?> /></span>
						<span class="star_2"><img src="<?php echo base_url() ?>images/star_blank.png" alt="" <?php if($ratingAvg > 1.5) { echo"class='hover_star'"; } ?> /></span>
						<span class="star_3"><img src="<?php echo base_url() ?>images/star_blank.png" alt="" <?php if($ratingAvg > 2.5) { echo"class='hover_star'"; } ?> /></span>
						<span class="star_4"><img src="<?php echo base_url() ?>images/star_blank.png" alt="" <?php if($ratingAvg > 3.5) { echo"class='hover_star'"; } ?> /></span>
						<span class="star_5"><img src="<?php echo base_url() ?>images/star_blank.png" alt="" <?php if($ratingAvg > 4.5) { echo"class='hover_star'"; } ?> /></span>
						<?php echo "Average: ".round($ratingAvg, 2); ?>
					</div>
				</div>
			</div>
		</br>
		
		<?php echo add_this();?>
		<hr />
	<?php endif; ?>
		
	<?php if(isset($isLoggedIn)): ?>
	<?php if($isLoggedIn): ?>

	<table>
	<tr>
	<td>
	<div id=new_comment">
		<p>
		<?php echo form_open('articles/newComment'); ?>
		<?php echo form_label('Comment: ', 'comment'); ?>
		</br>
		<?php echo form_textArea($textdata); ?>
		</br>
		<?php echo form_hidden('articleID', $article->articleID); ?>
		<?php echo form_submit('submit','Submit'); ?>
		<?php echo form_reset('reset', 'Clear'); ?>
		</p>
	</div>
	</td>
	<td>
	<div id=new_rate">

	</div>
	</td>
	</tr>
	</table>
	<?php endif; ?>
	<?php endif; ?>
	
	<div id="comments">
	<h3>Comments:</h3>
	<?php if(isset($comments)) :?>
	<?php foreach($comments as $comment) : ?>
	<table class="comment_Table">
	<tr>
	<td class="comment_Author"><?php echo $comment['author']; ?></td>
	<td class="comment_Date"><?php echo $comment['date']; ?></td>
	</tr>
	<tr>
	<td class="comment_Text" colspan="2"><?php echo $comment['text']; ?></td>
	</tr>
	</table>
	<?php endforeach; ?>
	<?php endif; ?>
	</div>

	
</div>